package com.rising.face.ali.bishi.findmaxstr;

import java.util.Arrays;

/**
 * @Author: rising
 * @Description:
 * @Date: create in 2021/4/10 21:23
 */

/**
 * 01234
 * 01234
 * 01234
 */
public class ZhengFang {

    public static void main(String[] args) {
        int[] x = {1,3};
        int[] y = {1,4};
        int[] z = {2,3};
        int[] w = {2,4};
        System.out.println(isZhengFang(x, y, z, w));
    }

    //入参为四个点,长度为2的以为数组，代表下标
    public static boolean isZhengFang(int x[], int y[], int z[], int w[]) {
        int p[][] = {{x[0], x[1]},
                {y[0], y[1]},
                {z[0], z[1]},
                {w[0], w[1]}
        };
        int cnt = 0;
        int len[] = new int[6];//用来保存线段长度，两个对角线和四条边
        for (int i = 0; i <=3; i++) {
            for (int j = i + 1; j <= 3; j++) {
                // p[i][0]是第i个点的x坐标；p[j][1]是第j个点的y坐标
                len[cnt++] = (p[i][0] - p[j][0]) * (p[i][0] - p[j][0]) + (p[i][1] - p[j][1]) * (p[i][1] - p[j][1]);

            }

        }

        //排序后，4、5是对角线，0、1、2、3是边
        Arrays.sort(len);
        //正方形的定义：相邻两边相等，对角线相等的四边形就是正方形
        if (len[0] == len[1] && len[1] == len[2] && len[2] == len[3]
                && len[4] == len[5] && len[4] > len[1]) {
            return true;
        }

        return false;

    }
}
